package com.thread.exem;

/**
 * @author げっしょく
 * @date 2021/8/6
 */
public class Test5 {
    public static void main(String[] args) {
        MyTicket myTicket = new MyTicket();
        Thread t1 = new Thread(myTicket,"窗口一");
        Thread t2 = new Thread(myTicket,"窗口二");
        Thread t3 = new Thread(myTicket,"窗口三");
        t1.start();
        t2.start();
        t3.start();

    }
}


class MyTicket implements Runnable{
    private int ticket = 100;
    @Override
    public void run() {
        while (true){
            synchronized (this){
                if (ticket > 0){
                    ticket--;
                    System.out.println(Thread.currentThread().getName() + "正在卖票 ，现在余票为" + ticket);
                }else {
                    return;
                }
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
